<template>
  <div>
    <div class="common-seach-wrap">
      <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="品牌名称">
          <el-input size="small" v-model="searchForm.name" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="Search" @click="getTableList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-level-rail">
      <el-button size="small" type="primary" @click="addSecurity">添加</el-button>
    </div>
    <div class="table-wrap">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="name" label="品牌名称"></el-table-column>
        <el-table-column prop label="图片">
          <template #default="scope">
            <img v-img-url="scope.row.image" alt :width="50" />
          </template>
        </el-table-column>
        <!-- <el-table-column prop="description" label="服务描述"></el-table-column> -->
        <el-table-column prop="status" label="是否发布">
          <template #default="scope">
            <span v-if="scope.row.push == 0">否</span>
            <span v-if="scope.row.push == 1">是</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="更新时间" width="180"></el-table-column>
        <el-table-column prop="name" label="操作" width="150">
          <template #default="scope">
            <el-button @click="editSecurity(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="deleteSecurity(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--添加-->
      <Add v-if="open_add" :open_add="open_add" @closeDialog="closeDialogFunc($event, 'add')"></Add>

      <!--编辑-->
      <Edit
        v-if="open_edit"
        :open_edit="open_edit"
        :editform="userModel"
        @closeDialog="closeDialogFunc($event, 'edit')"
      ></Edit>
    </div>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { useUserStore } from "@/store";
const { token } = useUserStore();
import PorductApi from "@/api/product.js";
import SupplierApi from "@/api/supplier.js";
import Edit from "./com/Edit.vue";
import Add from "./com/ADD.vue";
import { deepClone } from "@/utils/base.js";
export default {
  components: {
    /*编辑组件*/
    Edit,
    Add
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 15,

      /*当前是第几页*/
      curPage: 1,
      totalDataNumber: 0,
      tableData: [],
      /*分类*/
      categoryData: [],
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      commentData: [],
      /*是否加载完成*/
      loading: true,
      searchForm: {
        score: 0,
        name: ""
      }
    };
  },
  created() {
    /*获取文章列表*/
    this.getTableList();
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      let Params = self.searchForm;
      self.getTableList(Params);
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTableList();
    },
    /*获取文章列表*/
    getTableList(param = "") {
      let self = this;
      let Params = {};

      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      if (param != "") {
        Params.score = this.searchForm.score;
        Params.name = this.searchForm.name;
      }
      PorductApi.brandIndex(Params, true)
        .then(res => {
          console.log(res, "res");
          self.loading = false;
          self.tableData = res.data.records;
          self.totalDataNumber = res.data.total;
          // self.categoryData = res.data;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*删除*/
    deleteSecurity(row) {
      let self = this;
      ElMessageBox.confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          self.loading = true;
          PorductApi.brandDelet(
            {
              brandId: row.id
            },
            true
          )
            .then(data => {
              self.loading = false;
              ElMessage({
                message: data.msg,
                type: "success"
              });
              self.getTableList();
            })
            .catch(error => {
              self.loading = false;
            });
        })
        .catch(() => {});
    },
    handleClick(tab, event) {},

    /*打开添加*/
    addSecurity() {
      this.open_add = true;
    },

    /*打开编辑*/
    editSecurity(item) {
      this.userModel = deepClone(item);
      this.open_edit = true;
    },

    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == "add") {
        this.open_add = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
      if (f == "edit") {
        this.open_edit = e.openDialog;
        if (e.type == "success") {
          this.getTableList();
        }
      }
    }
  }
};
</script>

<style></style>
